package net.adst.util;

import net.adst.beans.NaemTec;
import net.adst.beans.OurTecRow;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.TreeSet;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 09.11.12
 * Time: 3:29
 * To change this template use File | Settings | File Templates.
 */
public class MainDataBuilder {


    public static void buildData() {
        try {

            InputStream in = MainDataBuilder.class.getClassLoader().getSystemResourceAsStream("work.xls");
            // Внимание InputStream будет закрыт
            // Если нужно не закрывающий см. JavaDoc по POIFSFileSystem :  http://goo.gl/1Auu7
            Biff8EncryptionKey.setCurrentUserPassword("adst-7777777");
            HSSFWorkbook wb = null;
            wb = new HSSFWorkbook(in);
            ExcelExtractor extractor = new ExcelExtractor(wb);
            extractor.setFormulasNotResults(false); // Считать формулы
            buildNaemTec(wb.getSheetAt(0));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }


    private static void buildNaemTec(HSSFSheet sheet) {
        ArrayList<NaemTec> naemTecs = new ArrayList<NaemTec>();
        TreeSet<String> tecModelNames = new TreeSet<String>();
        TreeSet<String> managerNames = new TreeSet<String>();
        TreeSet<String> customerNames = new TreeSet<String>();
        for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
            if (sheet.getRow(i) != null && MyUtils.isDate(sheet.getRow(i).getCell(0))) {
                NaemTec tec = new NaemTec(sheet.getRow(i));
                if (!tec.isEmpty()) {
                    naemTecs.add(tec);
                    tecModelNames.add(tec.getTecName());
                    managerNames.add(tec.getManagerName());
                    customerNames.add(tec.getCustomer());
                }
            }
        }
        DataManager.setObject(VariableNames.TEC_NAEM_ALL, naemTecs);

        JComboBox tecNameCombo = (JComboBox) DataManager.getObject(VariableNames.UI_NAEM_TECNAMECOMBO);
        ArrayList<String> tecNameList = new ArrayList<String>();
        ActionListener[] tecNameListeners = tecNameCombo.getActionListeners();
        for (ActionListener listener : tecNameListeners) {
            tecNameCombo.removeActionListener(listener);
        }
        DefaultComboBoxModel namesTecModel = (DefaultComboBoxModel) DataManager.getObject(VariableNames.UI_NAEM_TECNAMECOMBO_MODEL);
        namesTecModel.removeAllElements();
        namesTecModel.addElement(Constants.ALL_TEC);
        for (String tecModelName : tecModelNames) {
            namesTecModel.addElement(tecModelName);
            tecNameList.add(tecModelName);
        }

        DataManager.setObject(VariableNames.TEC_NAEM_NAME_LIST, tecNameList);
        for (ActionListener listener : tecNameListeners) {
            tecNameCombo.addActionListener(listener);
        }

        JComboBox managerCombo = (JComboBox) DataManager.getObject(VariableNames.UI_NAEM_MANAGERCOMBO);
        ActionListener[] managerListeners = managerCombo.getActionListeners();
        ArrayList<String> managerList = new ArrayList<String>();
        for (ActionListener listener : managerListeners) {
            managerCombo.removeActionListener(listener);
        }
        DefaultComboBoxModel managerModel = (DefaultComboBoxModel) DataManager.getObject(VariableNames.UI_NAEM_MANAGERCOMBO_MODEL);
        managerModel.removeAllElements();
        managerModel.addElement(Constants.ALL_MANAGERS);
        for (String managerName : managerNames) {
            managerModel.addElement(managerName);
            managerList.add(managerName);
        }
        DataManager.setObject(VariableNames.NAEM_MENAGER_LIST, managerList);
        for (ActionListener listener : managerListeners) {
            managerCombo.addActionListener(listener);
        }

        JComboBox customerCombo = (JComboBox) DataManager.getObject(VariableNames.UI_NAEM_CUSTOMERCOMBO);
        ActionListener[] customerListeners = customerCombo.getActionListeners();
        for (ActionListener listener : customerListeners) {
            customerCombo.removeActionListener(listener);
        }
        DefaultComboBoxModel customerModel = (DefaultComboBoxModel) DataManager.getObject(VariableNames.UI_NAEM_CUSTOMERCOMBO_MODEL);
        customerModel.removeAllElements();
        customerModel.addElement(Constants.ALL_CUSTOMERS);
        for (String customerName : customerNames) {
            customerModel.addElement(customerName);
        }
        for (ActionListener listener : customerListeners) {
            customerCombo.addActionListener(listener);
        }
    }


    public static void buildOurTec(HSSFWorkbook wb) {
        ArrayList<OurTecRow> OurTec = new ArrayList<OurTecRow>();
        TreeSet<String> tecModelNames = new TreeSet<String>();
        TreeSet<String> surnames = new TreeSet<String>();
        TreeSet<String> customerNames = new TreeSet<String>();
        for (int sh = 1; sh < wb.getActiveSheetIndex(); sh++) {
            HSSFSheet sheet = wb.getSheetAt(sh);
            for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
                if (sheet.getRow(i) != null && MyUtils.isDate(sheet.getRow(i).getCell(0))) {
                    OurTecRow tec = new OurTecRow(sheet.getRow(i), sheet.getSheetName());
                    if (!tec.isEmpty()) {
                        OurTec.add(tec);
                        surnames.add(tec.getSureName());
                        customerNames.add(tec.getCustomer());
                    }
                }
            }
            tecModelNames.add(sheet.getSheetName());
        }

        DataManager.setObject(VariableNames.TEC_OUR_ALL, OurTec);

        JComboBox tecNameCombo = (JComboBox) DataManager.getObject(VariableNames.UI_OUR_TECNAMECOMBO);
        ArrayList<String> tecNameList = new ArrayList<String>();
        ActionListener[] tecNameListeners = tecNameCombo.getActionListeners();
        for (ActionListener listener : tecNameListeners) {
            tecNameCombo.removeActionListener(listener);
        }
        DefaultComboBoxModel namesTecModel = (DefaultComboBoxModel) DataManager.getObject(VariableNames.UI_OUR_TECNAMECOMBO_MODEL);
        namesTecModel.removeAllElements();
        namesTecModel.addElement(Constants.ALL_TEC);
        for (String tecModelName : tecModelNames) {
            namesTecModel.addElement(tecModelName);
            tecNameList.add(tecModelName);
        }

        DataManager.setObject(VariableNames.TEC_OUR_NAME_LIST, tecNameList);
        for (ActionListener listener : tecNameListeners) {
            tecNameCombo.addActionListener(listener);
        }

        JComboBox surnameCombo = (JComboBox) DataManager.getObject(VariableNames.UI_OUR_SURNAMECOMBO);
        ActionListener[] managerListeners = surnameCombo.getActionListeners();
        ArrayList<String> surnameList = new ArrayList<String>();
        for (ActionListener listener : managerListeners) {
            surnameCombo.removeActionListener(listener);
        }
        DefaultComboBoxModel surnameModel = (DefaultComboBoxModel) DataManager.getObject(VariableNames.UI_OUR_SURNAMECOMBO_MODEL);
        surnameModel.removeAllElements();
        surnameModel.addElement(Constants.ALL_MANAGERS);
        for (String surname : surnames) {
            surnameModel.addElement(surname);
            surnameList.add(surname);
        }
        DataManager.setObject(VariableNames.OUR_SURNAME_LIST, surnameList);
        for (ActionListener listener : managerListeners) {
            surnameCombo.addActionListener(listener);
        }

        JComboBox customerCombo = (JComboBox) DataManager.getObject(VariableNames.UI_OUR_CUSTOMERCOMBO);
        ActionListener[] customerListeners = customerCombo.getActionListeners();
        for (ActionListener listener : customerListeners) {
            customerCombo.removeActionListener(listener);
        }
        DefaultComboBoxModel customerModel = (DefaultComboBoxModel) DataManager.getObject(VariableNames.UI_OUR_CUSTOMERCOMBO_MODEL);
        customerModel.removeAllElements();
        customerModel.addElement(Constants.ALL_CUSTOMERS);
        for (String customerName : customerNames) {
            customerModel.addElement(customerName);
        }
        for (ActionListener listener : customerListeners) {
            customerCombo.addActionListener(listener);
        }

    }
}
